package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.service.RentService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/rent")
public class RentController {
    @Autowired
    private RentService rs;

    @RequestMapping("/carInit")
    public String carInit() {
        return "/system/rent";
    }

    @RequestMapping("/carShow")
    @ResponseBody
    public AJAXResult carShow(CarVo carVo) {
        if (carVo.getPage() == null) {
            carVo.setPage(1);
        }
        if (carVo.getLimit() == null) {
            carVo.setLimit(5);
        }
        PageInfo pageInfo = rs.findByWhere(carVo);
        return new AJAXResult(0, "", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping("/addCar")
    @ResponseBody
    public AJAXResult addCar(CarVo carVo) {
        int row = rs.insertCar(carVo);
        if (row == 0) {
            return AJAXResult.ADD_ERROR;
        }
        return AJAXResult.ADD_SUCCESS;
    }

    @RequestMapping("/updateCar")
    @ResponseBody
    public AJAXResult updateCar(CarVo carVo) {
        int row = rs.updateCar(carVo);
        if (row == 0) {
            return AJAXResult.UPDATE_ERROR;
        }
        return AJAXResult.UPDATE_SUCCESS;
    }

    @RequestMapping("/carDelete")
    @ResponseBody
    public AJAXResult carDelete(String carnumber) {
        int row = rs.deleteByCarNumber(carnumber);
        if (row == 0) {
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
    }

    @RequestMapping("/deleteAllCar")
    @ResponseBody
    public AJAXResult deleteAllCar(String ids) {
        String[] carnumber = ids.split(",");
        int row = 0;
        for (int i = 0; i < carnumber.length; i++) {
            row += rs.deleteByCarNumber(carnumber[i]);
        }
        if (row == 0) {
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public AJAXResult uploadImg(@RequestParam("f1") MultipartFile file) {
        // 在IE11/Edge浏览器下面，获取到的路径信息带有盘符
        //文件名称
        try {
            String filename = file.getOriginalFilename();
            // 文件上传时，Chrome和IE/Edge对于originalFilename处理不同
            // Chrome 会获取到该文件的直接文件名称，IE/Edge会获取到文件上传时完整路径/文件名
            // Check for Unix-style path
            int unixSep = filename.lastIndexOf('/');
            // Check for Windows-style path
            int winSep = filename.lastIndexOf('\\');
            int pos = (winSep > unixSep ? winSep : unixSep);
            if (pos != -1) {
                filename = filename.substring(pos + 1);
            }

            String path = ResourceUtils.getURL("classpath:").getPath() + "static/photo/" + filename;

            file.transferTo(new File(path));
            Map<String,Object> map=new HashMap<>();
            map.put("src","/static/photo/"+filename);
            return new AJAXResult(map);
        }
        catch(Exception e){
            return new AJAXResult(2,e.getMessage());
        }
    }


}